from HTMLTable import HTMLTable
class HtmlTableApi(object):

    def __init__(self,title:str,header_data:tuple,rows_data:tuple):
        self.title = title
        self.table = HTMLTable(caption=title,rows=())
        self.table.append_header_rows(rows=header_data)
        self.table.append_data_rows(rows=rows_data)

    def setStyle(self,align ="center",cursor = "pointer",color='#2F7F9E'):

        caption_style = {
            'text-align': align,
            'cursor': cursor,
            "color" : color,
            "margin":'2px auto 6px auto'
        }
        self.table.set_style(style=caption_style)

    def setCellStyle(self):

        cell_style = {
            'text-align': 'center',
            'padding': '3px',
            'color':'#F95738',
            'font-size': '0.95em',
            'width':'300px'
        }
        border_style = {
                'border-color': '#000',
                'border-width': '1px',
                'border-style': 'solid',
                'border-collapse': 'collapse',
                # 实现表格居中
                'margin': 'auto',
            }
        self.table.set_style(border_style)
        self.table.set_cell_style(border_style)
        self.table.set_cell_style(cell_style)

    def setTableHeaderStyle(self):
        header_cell_style = {
            'text-align': 'center',
            'padding': '10px',
            'background-color': '#F5F5F5',
            'color': '#4A90E2',
            'font-size': '1em',
        }
        self.table.set_header_cell_style(header_cell_style)

    def setAllStyle(self):
        self.setCellStyle()
        self.setTableHeaderStyle()
        self.setStyle(cursor="hand")

    def createHtml(self,nowtime,htmlname):
        html = self.table.to_html().replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", '"')
        # 定义需要移除的非法字符列表
        illegal_chars = ['\ufffd', '\u200e', '\u200f', '\u202a', '\u202b',
                         '\u202c', '\u202d', '\u202e', '\u2066', '\u2067',
                         '\u2068', '\u2069']
        # 使用循环替换非法字符
        for char in illegal_chars:
            html = html.replace(char, '')
        with open(f"./result/{nowtime}截屏探测/{htmlname}.html","wt",encoding="utf-8",errors='ignore') as f:
            f.write(html)

def convertinfo(urls,codes,titles,imgpaths,nowtime):
    urlinfo=[]
    paths=[]
    for url,code,title,imgpath in list(zip(urls,codes,titles,imgpaths)):
        url=f"<a href=\"{url}\" target=\"_blank\">{url}</a>"
        info=url+"<br/>"+str(code)+"<br/>"+title
        if imgpath=='无法访问':
            imgpath = f"<img src=../../img/{nowtime}/{imgpath} style=\"width:50px\">"
        else:
            imgpath=f"<a href=../../img/{nowtime}/{imgpath} target=\"_blank\"><img src=../../img/{nowtime}/{imgpath} style=\"width:850px\"></a>"
        paths.append(imgpath)
        urlinfo.append(info)
    return tuple(zip(urlinfo,paths))

def writehtml(urls,codes,titles,imgpaths,nowtime,htmlname):
    try:
        print('正在生成html报告！')
        rows_data=convertinfo(urls,codes,titles,imgpaths,nowtime)
        html = HtmlTableApi(title="<span style=font-size:18px>截图报告</span>",
                                rows_data=rows_data,
                                header_data=(('url信息','对应截图'),))
        html.setAllStyle()
        html.createHtml(nowtime,htmlname)
        print('html报告已生成')
    except Exception as e:
        print('出错了')
        print(e)



